const pool = require('./db.js')
class riskDao {
  // 根据农田的id来查询农田的信息与建议
  async getSuggest(farmLandId){
    try{
      const result = await pool.execute('SELECT * FROM farmland_risk WHERE farmland_id = ?',[farmLandId])
      return result
    }catch(err){
      console.log('出错：'+ err)
    }
  }
  // 根据风险等级来获取农田信息与建议 只获取高风险等级的建议
  async getRiskSuggest(){
    try{
      const result = await pool.execute('SELECT * FROM farmland_risk WHERE risk_level = "高"')
      return result
    }catch(err){
      console.log('出错：'+ err)
    }
  }
  // 根据农田id修改农田建议完成状态
  async modifyState(farmland_id,state){
    try {
      const result = await pool.execute('UPDATE farmland_risk set state=? where farmland_id = ?',[state,farmland_id])
      return result
    }catch(err){
      console.log('出错：'+ err)
    }
  }
  // 根据农田的id 存入该农田的风险等级以及改良建议
  async setDevice(farmland_id,risk_level,suggest){
    try{
      const result = await pool.execute('insert farmland_risk(farmland_id,risk_level,suggest,state) value(?,?,?,0)',[farmland_id,risk_level,suggest])
    }catch(err){
      console.log('出错' + err)
    }
  }
}
module.exports = new riskDao()